﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;

namespace EasyComm
{
    [CollectionDataContract]
    public class ServiceContractCollection : List<ServiceContract>
    {
        public ServiceContract this[Type contractType]
        {
            get
            {
                return this.Where(contract => contract.ContractType.Equals(contractType)).FirstOrDefault();
            }
        }

        public new void Add(ServiceContract item)
        {
            if (base.ToArray().Count(baseItem => baseItem.ContractName == item.ContractName) == 0)
            {
                base.Add(item);
            }
        }

        //public new void AddRange(IEnumerable<ServiceContract> collection)
        //{
        //    base.AddRange(collection.Where(item => base.ToArray().Count(baseItem => baseItem.ContractName == item.ContractName) == 0).ToList());
        //}

        //public void AddRange(IEnumerable<Type> collection)
        //{
        //    collection.Where(item => base.ToArray().Count(baseItem => baseItem.ContractName == item.FullName) == 0).ToList().ForEach(contract => base.Add(new ServiceContract(contract)));
        //}

        internal Type[] ToTypeArray()
        {
            return base.ToArray().Select(item => item.ContractType).ToArray();
        }
    }
}
